<?php 
$account = new Account();
$account = $account->get(Session::get('account_id'));

$chipList = new ChipList();
$chipList = $chipList->getByAccountId($account->id);

$chip = new Chip();
$chip = $chip->get(Session::get('chip_id'));

$visited_pages = unserialize($chip->visited_pages);
if($visited_pages['saldo'] == 0):
$visited_pages['saldo'] = 1;
$chip->visited_pages = serialize($visited_pages);
$chip->save();
?>
	<script>
	$(function() {
		$("#info-form").dialog( "option", "width", 500 );
		$("#info-form").dialog("open");
	});
	</script>
	<div id="info-form" title="Uitleg over saldo">
		<p>Met uw saldo kunt u (en uw bekenden/kinderen) binnen het park betalen bij de diverse restaurants, snoep- en souvenirwinkels. Waardeer uw saldo op aan de rechterkant en volg de instructies. Uw saldo wordt automatisch verhoogd. Verdeel uw opgewaardeerde saldo in het linkerkant van het scherm. Vul achter de namen het gewenste saldo in voor die persoon en druk op "opslaan".</p>
	</div>
<? endif; ?>
<?php
function calculateAccountCredit($chipList) {
	$credit = 0;
	foreach($chipList as $chip) {
		$credit = $credit + $chip->credit;
	}
	return $credit;
}

if(isset($_POST['credit'])) {
	foreach($_POST['credit'] as $key => $credit) {
		$chip = new Chip();
		$chip->get($key);
		$chip->credit = $credit;
		$chip->save();
	}
	$chip = new Chip();
	$chip = $chip->get(1);
	
	$account = new Account();
	$account = $account->get(1);
	
	$chipList = new ChipList();
	$chipList = $chipList->getByAccountId($account->id);
	
	$error = "Uw saldo is succesvol verdeeld";
}


?>
<script type="text/javascript">
$('document').ready( function () {
	
	$('.chip_saldo').keyup(function () {

		if($(this).val() < 0){
			alert("Het getal mag niet negatief zijn");
			$(this).val('');
		}
		else{
			their_saldo = theirSaldo()
			$chip_saldo = $('.chip_saldo');
			var account_credit = parseInt($('input[name=total_credit]').val());
		}

		
		if(account_credit >= their_saldo  ) {
			mysaldo = account_credit - their_saldo;
			setMySaldo(mysaldo);
			$(this).css('border', '1px solid green');
		} else {
			$(this).css('border', '1px solid red');
		}

		
	});

	
	function theirSaldo() {
		total = 0;
		$('.chip_saldo').each(function () {
			total = total + parseInt($(this).val());
		});
		return total;
	}

	function getMySaldo() {
		mysaldo = parseInt($('.my_saldo').val());
		return mysaldo;
	}

	function setMySaldo(saldo) {
		$('.my_saldo').val(saldo);
	}


	var pinpasAsked = false;
	$('#opwaarderen-success').hide();
	$('#pinpas').hide();
	$('#pincode').hide();

	$('#opwaarderen-knop').click(function(){

		if(($("#pinpas").not(":visible")) && (pinpasAsked == false)){

			if($('input[name=bedrag]').val() == ''){
				alert("Vul een opwaardeerbedrag in");
			}
			else{
				$('input[name=bedrag]').attr('readonly', 'readonly');
				pinpasAsked = true;
			
				$("#pinpas").fadeIn('slow', function(){

					setTimeout(function(){
						$("#pinpas").fadeOut('slow', function(){ 
	
							$("#pincode").fadeIn();
							});
			        }, 2000)
				
				});
			}
		}

		if($("#pincode").is(":visible")){
			
			if($('input[name=pincode]').val() == ""){
				alert("Vul je pincode in");
			}
			else if($('input[name=pincode]').val().length != 4){
				alert("Je pincode bestaat uit vier cijfers");
			}
			else{
				pinpasAsked = false;
	
				var bedrag = parseInt($('#opwaardeerbedrag').val());
				var eigensaldo = parseInt($('#my_saldo').val());
				var totaalsaldo = parseInt($('#total_saldo').html());
				var sumEigen = bedrag + eigensaldo;
				var sumTotaal = bedrag + totaalsaldo;

				$.post("/ajax/add_saldo.php", { chip_id: "<?php echo Session::get('chip_id') ?>", saldo: bedrag } );
				
				$('#my_saldo').val(sumEigen);
				$('#total_saldo').html(sumTotaal);
				
				$('#opwaarderen-success').show();
				
				$("#pinpas").fadeOut('slow');
				$("#pincode").fadeOut('slow', function(){
					
				});
				
				$('input[name=bedrag]').val('');
			}
		};
			


	
	});

	$('input[name=bedrag]').keyup(function () {
		if($(this).val() < 0){
			alert("Het getal mag niet negatief zijn");
			$(this).val('');
		}
	});

	$('input[name=pincode]').keyup(function () {

		if($(this).val() != "")
		{
			if($(this).val() < 0){
				alert("Het getal mag niet nul of lager zijn");
				$(this).val('');
			}
			else if($(this).val() != parseFloat($(this).val())){
				alert("Vul alleen getallen in");
				$(this).val('');
			};
		}
			
	});

});


</script>

<style>
.box.brown{
	background: #eedbb1;
}

table{
	width: 100%;
	border-collapse: collapse;
	}
table thead th{
	background: #c5ae84;
	}
table tbody td:first-child{
	width: 70%;
}
table th, table td{
	padding: 10px;
}

table thead th, table tbody td{
	border-bottom: 1px solid #b5a883;
	}
	
table tbody td{

	border-top: 1px solid #e7e2d4;
}

table h2{
	text-align: left !important;
	}


</style>
<div class="row">
	<div class="twelvecol box red last">
		<div class="outer_wrapper rounded">
			<div class="wrapper">
				<div class="top_nav">
					<button class="twocol" onClick="javascript: history.go(-1)">Terug</button>
					<h2>Saldo</h2>
				</div>
			</div>
		</div>
	</div>
		<div id="opwaarderen-success" class="twelvecol margin-top margin-bottom">
			<div class="success">U heeft uw saldo succesvol opgewaardeerd.</div>
		</div>
		
		<?php if(isset($error)):?>
		<div class="twelvecol margin-top margin-bottom">
			<div class="success"><?= $error ?></div>
		</div>
	<?php endif;?>
	<?php if($chip->super_user == 1): ?>
	<div class="fourcol box brown full-height">
		<form name="verdeel_saldo" method="post" action="<?= URL ?>saldo">
			<input type="hidden" name="total_credit" value="<?= calculateAccountCredit($chipList)?>" />
			<table>
				<thead>
					<tr>
						<th><h2>Totaal saldo</h2></th>
						<th><h2>&euro; <span id="total_saldo"><?= calculateAccountCredit($chipList); ?></span></h2></th>
					</tr>
				</thead>
				<tbody>
<?php foreach($chipList as $account_chip): ?>
	<?php if($account_chip->id == Session::get('chip_id')): ?>
<tr>
	<td><h2>Eigen saldo</h2></td>
	<td><input id="my_saldo" class="my_saldo eightcol" style="font-family: Efteling; font-size: 1.4em;" type="number" name="credit[<?= $account_chip->id ?>]" value="<?= $account_chip->credit ?>" readonly="readonly" /></td>
</tr>
	<?php elseif ($chip->super_user == 1): ?>	
<tr>
	<td><h2><?= $account_chip->name ?></h2></td>
	<td><input class="chip_saldo eightcol" style="font-family: Efteling; font-size: 1.4em;" type="number" name="credit[<?= $account_chip->id ?>]" value="<?= $account_chip->credit ?>" /></td>
</tr>
	<?php endif; ?>	
<?php endforeach; ?>
					
				</tbody>
			</table>
			<button class="margin-top margin-right" style="float: right">Opslaan</button>
		</form>
	</div>
	
	<div class="eightcol box last">
		<table>
			<thead>
				<tr>
					<th colspan="2"><h2>Mijn saldo opwaarderen</h2></th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td><h2>Op te waarderen saldo</h2></td>
					<td><input id="opwaardeerbedrag" type="number" name="bedrag" class="eightcol" style="font-family: Efteling; font-size: 1.4em; float: right" /></td>
				</tr>
				<tr id="pinpas">
					<td colspan="2"><h2>Voer uw pinpas in</h2></td>
				</tr>
				<tr id="pincode">
					<td><h2>Voer uw pincode in</h2></td>
					<td><input type="password" name="pincode" class="eightcol" style="font-family: Efteling; font-size: 1.4em; float: right" /></td>
				</tr>
			</tbody>
		</table>
			<button id="opwaarderen-knop" class="margin-top margin-right" style="float: right">Verder</button>
	</div>
	<?php else: ?>
	<div class="fourcol box brown full-height">
			<table>
				<tbody>
				<tr>
					<td><h2>Eigen saldo</h2></td>
					<td><input class="my_saldo eightcol" style="font-family: Efteling; font-size: 1.4em;" type="number" name="credit[<?= $account_chip->id ?>]" value="<?= $chip->credit ?>" readonly="readonly" /></td>
				</tr>	
				</tbody>
			</table>
	<?php endif; ?>
</div>